What is kind-of?
The kind-of npm package is a utility library that allows developers to check the type of a given value in JavaScript. It provides a simple API to determine whether a value is a string, number, array, object, function, and other JavaScript types. This can be particularly useful for type checking and validation in applications where type safety is important.
What are kind-of's main functionalities?
Type checking for primitives
This feature allows you to check the type of primitive values such as numbers, strings, and booleans.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(1)); // 'number' console.log(kindOf('hello')); // 'string' console.log(kindOf(true)); // 'boolean'
Type checking for objects
This feature allows you to check the type of object-based values such as plain objects, arrays, and regular expressions.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf({})); // 'object' console.log(kindOf([])); // 'array' console.log(kindOf(/foo/)); // 'regexp'
Type checking for functions and undefined
This feature allows you to check the type of functions and the undefined value.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(function () {})); // 'function' console.log(kindOf(undefined)); // 'undefined'
Type checking for null and dates
This feature allows you to check the type of null values and date objects.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(null)); // 'null' console.log(kindOf(new Date())); // 'date'
Other packages similar to kind-of
type-check
The 'type-check' package offers a similar functionality to 'kind-of' by allowing type assertions and checks. It provides a mini language to specify the types of values and can validate against those specifications.
is
The 'is' package is another utility library that provides type checking functions. It has a wider range of type checks available, including checks for specific JavaScript environments like the browser or Node.js.
check-types
The 'check-types' package provides a set of predicates for type checking. It has a fluent API and includes additional checks for min/max values, empty strings, and arrays, which are not directly available in 'kind-of'.
kind-of
Get the native type of a value.
Install with npm
npm i kind-of --save
Usage
var kindOf = require('kind-of');
kindOf(undefined);
kindOf(null);
kindOf(true);
kindOf(false);
kindOf(new Boolean(true));
kindOf(new Buffer(''));
kindOf(42);
kindOf(new Number(42));
kindOf("string");
kindOf(arguments);
kindOf({});
kindOf(new Test());
kindOf(new Date());
kindOf([]);
kindOf([1, 2, 3]);
kindOf(new Array());
kindOf(/[\s\S]+/);
kindOf(new RegExp('^' + 'foo$'));
kindOf(function () {});
kindOf(new Function());
Run tests
Install dev dependencies:
npm i -d && npm test
Benchmarks
Benchmarked against typeof and type-of.
kind-of x 21,578,944 ops/sec ±1.01% (97 runs sampled)
(lib) type-of x 4,593,840 ops/sec ±0.76% (92 runs sampled)
(lib) typeof x 5,786,776 ops/sec ±0.71% (97 runs sampled)
kind-of x 25,189,600 ops/sec ±0.60% (97 runs sampled)
(lib) type-of x 2,751,076 ops/sec ±0.78% (100 runs sampled)
(lib) typeof x 4,390,312 ops/sec ±0.61% (99 runs sampled)
kind-of x 8,862,303 ops/sec ±0.77% (99 runs sampled)
(lib) type-of x 6,239,662 ops/sec ±0.67% (94 runs sampled)
(lib) typeof x 6,180,922 ops/sec ±0.59% (97 runs sampled)
kind-of x 19,685,336 ops/sec ±1.67% (95 runs sampled)
(lib) type-of x 6,648,551 ops/sec ±0.93% (95 runs sampled)
(lib) typeof x 6,631,967 ops/sec ±1.05% (92 runs sampled)
kind-of x 24,155,010 ops/sec ±0.95% (91 runs sampled)
(lib) type-of x 12,854,583 ops/sec ±0.69% (94 runs sampled)
(lib) typeof x 8,182,952 ops/sec ±0.48% (99 runs sampled)
kind-of x 20,993,521 ops/sec ±0.37% (98 runs sampled)
(lib) type-of x 2,112,716 ops/sec ±0.73% (96 runs sampled)
(lib) typeof x 4,492,943 ops/sec ±0.68% (96 runs sampled)
kind-of x 3,686,169 ops/sec ±0.85% (96 runs sampled)
(lib) type-of x 3,661,833 ops/sec ±0.73% (98 runs sampled)
(lib) typeof x 6,159,847 ops/sec ±0.72% (98 runs sampled)
kind-of x 10,780,535 ops/sec ±0.75% (95 runs sampled)
(lib) type-of x 5,380,781 ops/sec ±0.83% (92 runs sampled)
(lib) typeof x 5,852,558 ops/sec ±0.67% (95 runs sampled)
kind-of x 19,713,570 ops/sec ±0.69% (91 runs sampled)
(lib) type-of x 4,017,753 ops/sec ±0.85% (98 runs sampled)
(lib) typeof x 4,370,984 ops/sec ±0.62% (100 runs sampled)
kind-of x 23,250,387 ops/sec ±0.88% (91 runs sampled)
(lib) type-of x 13,725,183 ops/sec ±0.62% (91 runs sampled)
(lib) typeof x 20,549,334 ops/sec ±0.74% (97 runs sampled)
Author
Jon Schlinkert
License
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb on February 09, 2015.